/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.Color;
import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.*;
import mx.edu.itschapala.sistemas.septimo.modelo.*;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.*;

/**
 *
 * @author Miguel Romero
 */
public class ProductoCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ProductoCrearEditar
     */
    private ProductoLnLocal productoControl;
    private TipoVentana tipoVentana;
    private Producto producto;
    ProductoLnLocal productoLn = new ProductoLnLista();

    public ProductoCrearEditar(Producto producto, ProductoLnLocal productoControl, TipoVentana tipoVentana) {
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        this.producto=producto;
        
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel6.setText("REGISTRAR PRODUCTO");
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("EDITAR PRODUCTO");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                //BotonCancelar.setVisible(false);
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR UN PRODUCTO");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                //BotonCancelar.setVisible(false);
                //BotonCancelar.setText("NUEVO");
                break;
            case VENTANA_ELIMINAR:
                jLabel6.setText("ELIMINAR UN PRODUCTO");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                //BotonCancelar.setVisible(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jPanel9 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        BotonAceptar = new javax.swing.JButton();
        BotonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(7, 1));

        jPanel4.setLayout(new java.awt.GridLayout(1, 1));

        jLabel6.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel6.setText("TITULO VProducto");
        jPanel4.add(jLabel6);

        getContentPane().add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 2));

        jLabel1.setText("Codigo");
        jPanel5.add(jLabel1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel5.add(jTextField1);

        getContentPane().add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(1, 2));

        jLabel2.setText("Nombre");
        jPanel6.add(jLabel2);
        jPanel6.add(jTextField2);

        getContentPane().add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        jLabel3.setText("Precio");
        jPanel7.add(jLabel3);
        jPanel7.add(jTextField3);

        getContentPane().add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2));

        jLabel4.setText("Departamento");
        jPanel8.add(jLabel4);
        jPanel8.add(jTextField4);

        getContentPane().add(jPanel8);

        jPanel9.setLayout(new java.awt.GridLayout(1, 2));

        jLabel5.setText("Descripcion");
        jPanel9.add(jLabel5);
        jPanel9.add(jTextField5);

        getContentPane().add(jPanel9);

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        BotonAceptar.setText("Aceptar");
        BotonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAceptarActionPerformed(evt);
            }
        });
        jPanel1.add(BotonAceptar);

        BotonCancelar.setText("Cancelar");
        BotonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarActionPerformed(evt);
            }
        });
        jPanel1.add(BotonCancelar);

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_BotonCancelarActionPerformed

    private void BotonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAceptarActionPerformed
        // TODO add your handling code here:
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Producto p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.registrar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_BUSQUEDA:
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_MODIFICAR:
                p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.modificar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                jTextField1.setEditable(true);
                break;
                case VENTANA_ELIMINAR:
                p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.eliminar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
        }


    }//GEN-LAST:event_BotonAceptarActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
        if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                Producto p=productoControl.buscarPorCodigo(Integer.parseInt(jTextField1.getText()));
                if(p!=null){
                    jTextField2.setText(p.getNombre());
                    jTextField3.setText(p.getPrecio()+"");
                    jTextField4.setText(p.getDepartamento());
                    jTextField5.setText(p.getDescripcion());
                    if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                        jTextField2.setEditable(true);
                        jTextField3.setEditable(true);
                        jTextField4.setEditable(true);
                        jTextField5.setEditable(true);
                        jTextField1.setEditable(false);
                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAceptar;
    private javax.swing.JButton BotonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
